#include <cuda_runtime.h>
#include <device_launch_parameters.h>

#include "../image2d_ptr.h"

#include "lastError.h"

namespace Image2D
{

std::string LastError::m_lastErrorStr = "";
int         LastError::m_lastErrorCode = 0;
std::mutex  LastError::m_mutex;

int LastError::getLastErrorCode()
{
    return m_lastErrorCode;
}

std::string LastError::getLastErrorStr()
{
    return m_lastErrorStr;
}

void LastError::setLastError(std::string errorStr, int errorCode)
{
    m_mutex.lock();

    if (m_lastErrorCode != 0)
        return;

    if (errorCode == 0)
    {
        cudaDeviceSynchronize();
        m_lastErrorCode = cudaGetLastError();
        if(m_lastErrorCode != 0)
            m_lastErrorStr = errorStr +  "--" + cudaGetErrorString((cudaError_t)m_lastErrorCode);
    }
    else
    {
        m_lastErrorCode = errorCode;
        m_lastErrorStr = errorStr;
    }

    m_mutex.unlock();
}

}